package com.ruge.gateway.error;

import com.ruge.core.exception.BaseBusinessException;
import com.ruge.core.response.Response;
import com.ruge.core.tool.FormatTool;
import com.ruge.tool.json.JsonTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * @program: ruge-cloud
 * @description:
 * @author: ruge.wu
 * @create: 2022-01-08 20:37
 **/
@Slf4j
@Component
@Order(-1)
public class JsonExceptionHandler implements ErrorWebExceptionHandler {


    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        ServerHttpResponse response = exchange.getResponse();
        if (response.isCommitted()) {
            return Mono.error(ex);
        }
        // header set
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        if (ex instanceof ResponseStatusException) {
            response.setStatusCode(((ResponseStatusException) ex).getStatus());
        }
        return response
                .writeWith(Mono.fromSupplier(() -> {
                    DataBufferFactory bufferFactory = response.bufferFactory();
                    DataBuffer wrap = null;
                    if (ex instanceof BaseBusinessException) {
                        wrap = bufferFactory.wrap(FormatTool.buildJson((BaseBusinessException) ex).getBytes(StandardCharsets.UTF_8));
                        System.out.println("wrap=============request===============" + exchange.getRequest().getRemoteAddress());
                        System.out.println("wrap=============response===============" + exchange.getResponse().getStatusCode());
                    } else {
                        Response resultInfo = new Response();
                        resultInfo.setStatus(Response.Status.FAILED);
                        resultInfo.setErrorCode("SYS." + exchange.getResponse().getRawStatusCode());
                        resultInfo.setErrorMessage(exchange.getResponse().getStatusCode().toString());
                        resultInfo.setExtMessage(ex.getMessage());
                        wrap = bufferFactory.wrap(JsonTool.getObjToJson(resultInfo).getBytes(StandardCharsets.UTF_8));
                        System.out.println("JsonExceptionHandler=============request===============" + exchange.getRequest().getRemoteAddress());
                        System.out.println("JsonExceptionHandler=============response===============" + exchange.getResponse().getStatusCode());
                    }
                    return wrap;
                }));

    }

}
